package com.yimils.core.goods.serivce.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yimils.core.goods.dto.HotelTypeAddReq;
import com.yimils.core.goods.dto.HotelTypeUpdateReq;
import com.yimils.core.goods.entity.HotelTypeEntity;
import com.yimils.core.goods.mapper.HotelTypeMapper;
import com.yimils.core.goods.serivce.HotelTypeService;
import com.yimils.core.goods.vo.HotelTypeRes;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@Service("hotelTypeService")
public class HotelTypeServiceImpl extends ServiceImpl<HotelTypeMapper, HotelTypeEntity> implements HotelTypeService {
    @Override
    public Boolean saveHotelType(HotelTypeAddReq hotelTypeAddReq) {
        HotelTypeEntity entity = new HotelTypeEntity();
        entity.setHotelTypeName(hotelTypeAddReq.getHotelTypeName());
        entity.setState(Boolean.FALSE);
        entity.setCreateTime(LocalDateTime.now());
        entity.setUpdateTime(LocalDateTime.now());
        return save(entity);
    }

    @Override
    public Boolean updateHotelType(HotelTypeUpdateReq hotelTypeUpdateReq) {
        QueryWrapper<HotelTypeEntity> qw = new QueryWrapper<>();
        qw.lambda().eq(HotelTypeEntity::getId, hotelTypeUpdateReq.getHotelTypeId()).eq(HotelTypeEntity::getState, Boolean.FALSE);
        HotelTypeEntity entity = getOne(qw);
        entity.setHotelTypeName(hotelTypeUpdateReq.getHotelTypeName());
        entity.setUpdateTime(LocalDateTime.now());
        return saveOrUpdate(entity);
    }

    @Override
    public Boolean deleteHotelType(List<Long> hotelTypeIds) {
        QueryWrapper<HotelTypeEntity> qw = new QueryWrapper<>();
        qw.lambda().in(HotelTypeEntity::getId, hotelTypeIds).eq(HotelTypeEntity::getState, Boolean.FALSE);
        List<HotelTypeEntity> hotelTypeEntities = list(qw);
        hotelTypeEntities.forEach(o -> {
            o.setState(Boolean.TRUE);
            o.setUpdateTime(LocalDateTime.now());
        });
        return saveOrUpdateBatch(hotelTypeEntities);
    }

    @Override
    public List<HotelTypeRes> listHotelType() {
        QueryWrapper<HotelTypeEntity> qw = new QueryWrapper<>();
        qw.lambda().eq(HotelTypeEntity::getState, Boolean.FALSE);
        List<HotelTypeEntity> hotelTypeEntities = list(qw);
        return hotelTypeEntities.stream().map(o -> {
            HotelTypeRes hotelTypeRes = new HotelTypeRes();
            hotelTypeRes.setHotelTypeName(o.getHotelTypeName());
            hotelTypeRes.setHotelTypeId(o.getId());
            return hotelTypeRes;
        }).collect(Collectors.toList());
    }

    @Override
    public List<HotelTypeEntity> listHotelType(List<Long> hotelTypeIds) {
        QueryWrapper<HotelTypeEntity> qw = new QueryWrapper<>();
        qw.lambda().eq(HotelTypeEntity::getId, hotelTypeIds).eq(HotelTypeEntity::getState, Boolean.FALSE);
        return list(qw);
    }
}
